//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private onAddToStage(event: egret.Event) {
        egret.lifecycle.addLifecycleListener((context) => {
           
            context.onUpdate = () => {
            }
        })
        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }
        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }
        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
        // const result = await RES.getResAsync("description_json")
        // this.startAnimation(result);
        await platform.login();
        const userInfo = await platform.getUserInfo();
        console.log(userInfo);
    }
    private async loadResource() {
        try {
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            await RES.loadGroup("preload", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }
    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene() {
        // let rect:egret.Shape= new egret.Shape();
        // let rect1:egret.Shape= new egret.Shape();
        // rect.graphics.lineStyle(1,0xff0000);
        // rect1.graphics.lineStyle(5,0xff0000);
        // rect.graphics.beginFill(0x00ff00,0.5);
        // rect1.graphics.beginFill(0x0000ff,1);
        // rect1.graphics.drawCircle(300,300,300)
        // rect.graphics.drawRect(0,0,200,200);
        // rect.graphics.endFill();
        // rect1.graphics.endFill();
        // this.addChild(rect);
        // this.addChild(rect1);

        // let rect2:egret.Shape= new egret.Shape();
        // rect2.graphics.lineStyle(5,0xff0000);
        // rect2.graphics.beginFill(0x0000ff,0);
        // rect2.graphics.drawCircle(150,150,50)
        // rect2.graphics.endFill();
        // this.addChild(rect2);

        //  let rect3:egret.Shape= new egret.Shape();
        // rect3.graphics.lineStyle(5,0xff0000);
        // rect3.graphics.beginFill(0x0000ff,0);
        // rect3.graphics.drawCircle(450,150,50)
        // rect3.graphics.endFill();
        // this.addChild(rect3);

        //  let rect4:egret.Shape= new egret.Shape();
        // rect4.graphics.lineStyle(5,0xff0000);
        // rect4.graphics.beginFill(0x0000ff,0);
        // rect4.graphics.drawRect(200,400,200,50)
        // rect4.graphics.endFill();
        // this.addChild(rect4);

        //绘制圆弧
        // let hudu:egret.Shape= new egret.Shape();
        // hudu.graphics.lineStyle(5,0xff0000);
        // hudu.graphics.beginFill(0xff0000,0.5);
        // hudu.graphics.drawArc(300,400,100,Math.PI*2,Math.PI,false);//false:顺时针，true：逆时针
        // hudu.graphics.endFill();
        // this.addChild(hudu);

        //绘制直线
        // let line:egret.Shape=new egret.Shape();
        // line.graphics.lineStyle(3,0xffff00);
        // line.graphics.moveTo(0,600);
        // line.$graphics.lineTo(100,600);
        // line.graphics.endFill();
        // this.addChild(line);
        let hang=Math.ceil(this.stage.stageHeight/Mymokuai.mymokuaiheigth);
        let lie=Math.ceil(this.stage.stageWidth/Mymokuai.mymokuaiheigth);
        console.log(hang,lie)
        for(var i=0;i<hang;i++){
            for(var j=0;j<lie;j++){ 
                 let gg:Mymokuai=new Mymokuai();
                  gg.x=200*j;
                  gg.y=200*i;
               this.addChild(gg);
            }
        }


        // let yuan:egret.Shape = new egret.Shape();
        // yuan.graphics.lineStyle(5,0x7719cb);
        // yuan.graphics.beginFill(0x000000,0);
        // yuan.graphics.drawCircle(0,0,50);
        // yuan.graphics.endFill();
        // this.addChild(yuan);

      
        // let juxing:egret.Shape = new egret.Shape();
        // juxing.graphics.lineStyle(5,0x4caf50);
        // juxing.graphics.beginFill(0xff0000);
        // juxing.graphics.drawRect(0,0,100,100);
        // juxing.graphics.endFill();
        // this.addChild(juxing);
        // juxing.anchorOffsetX = juxing.width;
        // juxing.x = this.stage.stageWidth;

      
        // let yuan1:egret.Shape = new egret.Shape();
        // yuan1.graphics.lineStyle(5,0xff0000);
        // yuan1.graphics.beginFill(0x000000,0);
        // yuan1.graphics.drawCircle(0,0,50);
        // yuan1.graphics.endFill();
        // this.addChild(yuan1);
        // yuan1.x = this.stage.stageWidth-55;
        // yuan1.y = this.stage.stageHeight-55;

        
        // let yuan2:egret.Shape = new egret.Shape();
        // yuan2.graphics.lineStyle(5,0xcc3e9f);
        // yuan2.graphics.beginFill(0xcc3e71,0.5);
        // yuan2.graphics.drawCircle(0,0,100);
        // yuan2.graphics.endFill();
        // this.addChild(yuan2);
        // yuan2.x = this.stage.stageWidth/2;
        // yuan2.y = this.stage.stageHeight/2;

        
        // let xian1:egret.Shape = new egret.Shape();
        // xian1.graphics.lineStyle(7,0xff0000);
        // xian1.graphics.moveTo(this.stage.stageWidth/2,0);
        // xian1.graphics.lineTo(this.stage.stageWidth/2,this.stage.stageHeight);
        // xian1.graphics.endFill();
        // this.addChild(xian1);
        

        // let xian2:egret.Shape = new egret.Shape();
        // xian2.graphics.lineStyle(7,0xff0000);
        // xian2.graphics.moveTo(0,this.stage.stageHeight/2);
        // xian2.graphics.lineTo(this.stage.stageWidth,this.stage.stageHeight/2);
        // xian2.graphics.endFill();
        // this.addChild(xian2);


        // let juxing:egret.Shape=new egret.Shape();
        //  juxing.graphics.lineStyle(5,0xff0000);
        //  juxing.graphics.beginFill(0xffffff,1);
        //  juxing.graphics.drawRect(0,0,50,200);
        //  juxing.x=300;
        //  juxing.y=300;
        //   juxing.skewY=-30;
        //  juxing.rotation=-30;
        //  juxing.graphics.endFill();
        //  this.addChild(juxing);

        // let line:egret.Shape=new egret.Shape();
        // line.graphics.lineStyle(5,0xffff00);
        // line.graphics.moveTo(200,480);
        // line.graphics.lineTo(398,480);
        // line.graphics.endFill();
        // this.addChild(line);
    
        // let line1:egret.Shape=new egret.Shape();
        // line1.graphics.lineStyle(5,0xffff00);
        // line1.graphics.moveTo(200,480);
        // line1.graphics.lineTo(300,300);
        // line1.graphics.endFill();
        // this.addChild(line1);
    }
}